package com.zhou.base.components.bs.common.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zhou.base.components.bs.common.util.SortFeildUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @author li.ke
 * @date 2020/12/28
 */
@ApiModel("分页参数")
@Data
public class PageParam {

    @ApiModelProperty(value = "页数", example = "1", position = Integer.MAX_VALUE - 2)
    @NotNull(message = "页数不能为空")
    private Integer pageNo = 1;
    @ApiModelProperty(value = "每页大小", example = "10", position = Integer.MAX_VALUE - 1)
    @NotNull(message = "每页大小不能为空")
    private Integer pageSize = 10;

    @ApiModelProperty(value = "排序字段", dataType = "Map", example = "{\"field1\":\"desc\",\"field2\":\"asc\"}",
            position = Integer.MAX_VALUE)
    private Map<String, String> sortFieldMap;

    /**
     * 由排序的字段生成的排序字符串,xml中需要使用${req.sortStr},不能使用#{req.sortStr}
     *
     * @return
     */
    public String getSortStr() {
        String sortStr = SortFeildUtil.getSortStr(getSortFieldMap(), getAllowOrderFields());
        return sortStr;
    }

    @JsonIgnore
    public List<String> getAllowOrderFields() {
        return null;
    }

    @JsonIgnore
    public IPage getPage() {
        return new Page(getPageNo(), getPageSize());
    }

}